package com.sgb.member.ui.activity

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.google.android.material.tabs.TabLayout
import com.sgb.member.R
import com.sgb.member.databinding.ActivityMemberTypeBinding
import com.sgb.member.entity.RemoveMemberBean
//import com.sgb.member.ui.adapter.MemberTypeAdapter
import com.swgk.core.base.AppActivity
import com.swgk.core.customview.XCustomTabLayout
import com.swgk.core.util.MToast
import com.swgk.core.view.selection.CommonPopEntity
import com.swgk.core.xrecyclerview.LoadingMoreFooter
import com.swgk.core.xrecyclerview.XRecyclerView

/**
 * 会员类型页面
 */
class MemberTypeActivity : AppActivity() {

    private lateinit var mBinding: ActivityMemberTypeBinding
    var mCurIndex = 0
 //  lateinit var adapter: MemberTypeAdapter
    private var mSearchText = "" // 搜索内容

    companion object {
        fun start(activity: Activity) {
            val intent = Intent(activity, MemberTypeActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val mTabData = mutableListOf<CommonPopEntity>().apply {
        add(CommonPopEntity("0", "供方会员"))
        add(CommonPopEntity("1", "需方会员"))
    }


    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_type)
        initSearchView()
        mBinding.tabLayout.tabMode = TabLayout.MODE_FIXED
        mBinding.tabLayout.setNormalColor(R.color.color_333333)
        mBinding.tabLayout.addOnTabSelectedListener(mTabData,
            object : XCustomTabLayout.SimpleSelectedListener() {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    mCurIndex = tab.position
                }
            })
        initRecycler();
    }


    private fun initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener { onBackPressed() }
        mBinding.activityBar.etView.imeOptions = EditorInfo.IME_ACTION_SEARCH
        mBinding.activityBar.etView.hint = "请输入会员类型"
        mBinding.activityBar.etView.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN) {
                if (TextUtils.isEmpty(mBinding.activityBar.etView.text.toString())) {
                    MToast.showToast(activity, "请输入会员类型")
                    return@OnKeyListener true
                }
                if (mSearchText == mBinding.activityBar.etView.text.toString()) {
                    MToast.showToast(activity, "请输入不同的搜索内容")
                    return@OnKeyListener true
                }
                mSearchText = mBinding.activityBar.etView.text.toString().trim()
                loadData()
            }
            false
        })
        mBinding.activityBar.etView.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (TextUtils.isEmpty(s)) {
                    mBinding.activityBar.tvSearchDelete.visibility = View.INVISIBLE
                } else {
                    mBinding.activityBar.tvSearchDelete.visibility = View.VISIBLE
                }
            }

            override fun afterTextChanged(s: Editable) {}
        })
        mBinding.activityBar.tvSearchDelete.setOnClickListener {
            mSearchText = ""
            mBinding.activityBar.etView.setText("")
            loadData()
        }
        mBinding.activityBar.tvSearch.setOnClickListener(View.OnClickListener {
            if (TextUtils.isEmpty(mSearchText)) {
                if (TextUtils.isEmpty(mBinding.activityBar.etView.text.toString())) {
                    MToast.showToast(activity, "请输入会员类型")
                    return@OnClickListener
                }
            }
            if (mSearchText == mBinding.activityBar.etView.text.toString()) {
                MToast.showToast(activity, "请输入不同的搜索内容")
                return@OnClickListener
            }
            mSearchText = mBinding.activityBar.etView.text.toString().trim()
            loadData()
        })
        mBinding.activityBar.tvSearchDelete.visibility = View.INVISIBLE
    }

    private fun loadData(isLoadMore: Boolean = false) {

    }

    private fun initRecycler() {
//        val textView: TextView = mBinding.emptyView.findViewById(R.id.no_data_count)
//        textView.visibility = View.VISIBLE
//        mBinding.recyclerView.setPullRefreshEnabled(true)
//        mBinding.recyclerView.setLoadingMoreEnabled(true)
//        val footer = mBinding.recyclerView.rootView as LoadingMoreFooter
//        footer.setNoMoreHint("已经到底啦~")
//        adapter = MemberTypeAdapter(null, ::callBack)
//        mBinding.recyclerView.adapter = adapter
//        mBinding.recyclerView.setLoadingListener(object : XRecyclerView.LoadingListener {
//            override fun onRefresh() {
//                loadData()
//            }
//
//            override fun onLoadMore() {
//                loadData(true)
//            }
//        })
    }

    fun callBack(bean: RemoveMemberBean) {

    }

}